package com.zhouxiaoge.方法参数;

/**
 * @author 周明阳
 * mail:gqzmy@outlook.com
 * 时间:2018-04-24 11:36
 */

public class MethodParamTest {
    /**
     * 方法参数共有两种类型：基本数据类型（数字，布尔值）和对象引用。
     * Java对对象采用的不是引用调用，实际上，对象引用进行的是值传递。
     */
    public static void main(String[] args) {
        //1、一个方法不修改一个基本数据类型的参数
        System.out.println("测试基本类型");
        double percent = 10;
        System.out.println("开始前: percent = " + percent);
        tripleValue(percent);
        System.out.println("结束后: percent = " + percent);
        //2、一个方法可以改变一个对象的参数的状态
        System.out.println("测试对象参数");
        Employee harry = new Employee("Harry", 5000);
        System.out.println("开始前: salary = " + harry.getSalary());
        tripleSalary(harry);
        System.out.println("结束后: salary = " + harry.getSalary());
        //3、一个方法不能让对象引用一个新的对象
        System.out.println("测试对象引用");
        Employee alice = new Employee("Alice", 7000);
        Employee bob = new Employee("Bob", 8000);
        System.out.println("开始前 a = " + alice.getName());
        System.out.println("开始前 b = " + bob.getName());
        swap(alice, bob);
        System.out.println("结束后 a = " + alice.getName());
        System.out.println("结束后 b = " + bob.getName());

    }

    private static void tripleValue(double x) {
        x = 3 * x;
        System.out.println("方法内: x = " + x);
    }

    private static void tripleSalary(Employee x) {
        x.raiseSalary(200);
        System.out.println("方法内: salary=" + x.getSalary());
    }

    private static void swap(Employee x, Employee y) {
        Employee tem = x;
        x = y;
        y = tem;
        System.out.println("方法内:x = " + x.getName());
        System.out.println("方法内: y = " + y.getName());
    }
}
